<?php
class Common_model extends MY_Model{
	
	function __construct()
	{
		parent::__construct();
		$this->load->model(array('existing_coverage_endowment_model','existing_coverage_term_model','existing_coverage_ilp_model','existing_coverage_wl_model'));
	}
	
	function return_value_by_year($year = null)
    {
    	$row    =    $this->db->select()->from('current_holdings')->where('customer_id',$_SESSION["customer"]["id"])->get()->row_array();//$this->current_holding_model->get_by('customer_id',$_SESSION["customer"]["id"]);
    	$y=0;
    	$n	=	$year-date('Y');
    	if($row){
    		$y_sb	=	$row['SB']*pow((1+($row['SBR']/100)),$n);
			$y_sb	=	round(floatval($y_sb));
			
			$y_ss	=	$row['SS']*pow((1+($row['SSR']/100)),$n);
			$y_ss	=	round(floatval($y_ss));
			
			$y_us	=	$row['US']*pow((1+($row['USR']/100)),$n);
			$y_us	=	round(floatval($y_us));
			
			$y_b	=	$row['B']*pow((1+($row['BR']/100)),$n);
			$y_b	=	round(floatval($y_b));
			
			$y_fd	=	$row['FD']*pow((1+($row['FDR']/100)),$n);
			$y_fd	=	round(floatval($y_fd));
			
			$y_ip	=	$row['IP']*pow((1+($row['IPR']/100)),$n);
			$y_ip	=	round(floatval($y_ip));
			
			$y_cpf	=	$row['CPF']*pow((1+($row['CPFR']/100)),$n);
			$y_cpf	=	round(floatval($y_cpf));
			
			$y=$y_sb+$y_ss+$y_us+$y_b+$y_fd+$y_ip+$y_cpf;
    	}
    	return $y;
    }
	
	function get_result_existing_coverage($year = null)
	{
		$endowments	=	$this->existing_coverage_endowment_model->get_many_by('customer_id',$_SESSION["customer"]["id"]);
		$terms	=	$this->existing_coverage_term_model->get_many_by('customer_id',$_SESSION["customer"]["id"]);
		$ilps	=	$this->existing_coverage_ilp_model->get_many_by('customer_id',$_SESSION["customer"]["id"]);
		$wls	=	$this->existing_coverage_wl_model->get_many_by('customer_id',$_SESSION["customer"]["id"]);
		
		$x_start	=	$year;
		$y_start=0;
		$y_start_sadi=0;
		$y_start_saci=0;
		foreach($endowments as $e){
			if($x_start <= ($e->policy_incepted+$e->policy_term)){
				$y_start+=($e->sum_assured_death);
				
				//sum_assured_disability
				$y_start_sadi+=($e->sum_assured_disability);
				//sum_assured_critical_illness
				$y_start_saci+=($e->sum_assured_critical_illness);
			}
		}
		
		//echo $y_end;
		
		foreach($terms as $t){
			if($x_start <= ($t->policy_incepted+$t->policy_term)){
				$y_start+=($t->sum_assured_death	-	round((($t->sum_assured_death/$t->policy_term)*($x_start-$t->policy_incepted))));
				
				//sum_assured_disability
				$y_start_sadi+=($t->sum_assured_disability	-	round((($t->sum_assured_disability/$t->policy_term)*($x_start-$t->policy_incepted))));
				//sum_assured_critical_illness
				$y_start_saci+=($t->sum_assured_critical_illness	-	round((($t->sum_assured_critical_illness/$t->policy_term)*($x_start-$t->policy_incepted))));
			}
			
		}
		
		foreach($wls as $w){
			if($x_start <= ($w->policy_incepted+$w->policy_term)){
				$y_start+=$w->sum_assured_death;
				//sum_assured_disability
				$y_start_sadi+=($w->sum_assured_disability);
				//sum_assured_critical_illness
				$y_start_saci+=($w->sum_assured_critical_illness);
			}
			if($w->step_up_booster==1){
				if(intval($x_start)==intval($w->year_start)){
					$y_start+=$w->addictional_sum_assured_death;
					//
					$y_start_sadi+=($w->addictional_sum_assured_disability);
					//sum_assured_critical_illness
					$y_start_saci+=($w->addictional_sum_assured_critical);
				}
			}
		
		}
		
		foreach($ilps as $i){
			if($x_start <= ($i->policy_incepted+$i->policy_term)){
				$y_start+=$i->sum_assured_death;
				//sum_assured_disability
				$y_start_sadi+=($i->sum_assured_disability);
				//sum_assured_critical_illness
				$y_start_saci+=($i->sum_assured_critical_illness);
			}
		}
		
		return array($y_start,$y_start_sadi,$y_start_saci);
	}
	
	function return_retirement_by_year($year = null)
	{
		$year_birday	=	date('Y',strtotime($_SESSION['customer']['date_of_birth']));
		$paramter	=	$this->data['paramter'];
		$year_retirement	=	$year_birday + ($paramter ? $paramter->retirement_age : 60);
		$cats_re	=	array();
		$endowments	=	$this->existing_coverage_endowment_model->get_many_by('customer_id',$_SESSION["customer"]["id"]);
		$terms	=	$this->existing_coverage_term_model->get_many_by('customer_id',$_SESSION["customer"]["id"]);
		$ilps	=	$this->existing_coverage_ilp_model->get_many_by('customer_id',$_SESSION["customer"]["id"]);
		$wls	=	$this->existing_coverage_wl_model->get_many_by('customer_id',$_SESSION["customer"]["id"]);
		
		$x_re_start	=	$year;
		$y_re_start	=	0;
		
		foreach($wls as $w)
		{
			$y_re_start+=$w->cva_retirement;
		}
		
		foreach($ilps as $i)
		{
			$y_re_start+=round($i->cva_retirement*(pow( (1 + ($i->expected_annual_return/100) ) ,(intval($x_re_start) - intval($year_retirement)))));
			
		}
		
		foreach($endowments as $e)
		{
			if($x_re_start==$e->maturity_year)
			{
				$y_re_start+=$e->maturity_value;
			}
		}
		
		return $y_re_start;
	}
}